﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace Fitment.User
{
    public partial class userInfo : System.Web.UI.Page
    {
        Userinfo user = new Userinfo();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                setButtonState();
                //加载该页面时把用户信息初始化在控件中，并可以修改功能，当用户点击修改时，可以修改用户信息，此时按钮变为确定
                user.Id = Convert.ToInt32(Session["User_ID"].ToString());
                user.findById();
                tb_acc.Text = user.Account;
                tb_name.Text = user.Name;
                dd_sex.Text = user.Sex;
                tb_tel.Text = user.Telphone.Trim();
                tb_email.Text = user.Eemail;
                tb_add.Text = user.Address;
                if (user.Age == 0)
                {
                    dd_age.Text = "请选择";
                }
                else
                {
                    dd_age.Text = user.Age.ToString();
                }
            }
        }
        /*修改用户信息并保存*/
        protected void ImageButton3_Click(object sender, ImageClickEventArgs e)
        {
            lb_succ.Text = "";
            if (ImageButton3.ImageUrl.Equals("~/images/modify.jpg"))
            {
                ImageButton3.ImageUrl = "~/images/summit.jpg";
                setButtonState();
            }
            else
            {
                //更新数据库中用户的信息
                string acc = tb_acc.Text.ToString().Trim();
                string name = tb_name.Text.ToString().Trim();
                string sex = dd_sex.Text.ToString().Trim();
                string strage = dd_age.Text.ToString().Trim();
                string tel = tb_tel.Text.ToString().Trim();
                string email = tb_email.Text.ToString().Trim();
                string add = tb_add.Text.ToString().Trim();
                int age = 0;
                if (strage.Equals("请选择"))
                {
                    age = 0;
                }
                else
                {
                    age = Convert.ToInt32(strage);
                }
                user.Id = Convert.ToInt32(Session["User_ID"].ToString());
                if (user.modifyInfo(acc, name, sex, age, tel, email, add))
                {
                    lb_succ.Text = "信息修改成功";
                    ImageButton3.ImageUrl = "~/images/modify.jpg";
                    setButtonState();
                }
                else
                {
                    lb_succ.Text = "信息修改不成功";
                }
            }
        }
      
        protected void setButtonState()
        {
            //查看状态
            if (ImageButton3.ImageUrl.Equals("~/images/modify.jpg"))
            {
                tb_name.Enabled = false;
                dd_sex.Enabled = false;
                dd_age.Enabled = false;
                tb_tel.Enabled = false;
                tb_email.Enabled = false;
                tb_add.Enabled = false;
            }
            //修改状态
            else
            {
                tb_name.Enabled = true;
                dd_sex.Enabled = true;
                dd_age.Enabled = true;
                tb_tel.Enabled = true;
                tb_email.Enabled = true;
                tb_add.Enabled = true;
                tb_name.Focus();
            }
        }
    }
}
